home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 May / EnigmA AMIGA RUN 18 (1997)(G.R. Edizioni)(IT)[!][issue 1997-05][EAR-CD II].iso / earcd / comm / tcp / ifcheck.lha / ifcheck.c < prev    next >
C/C++ Source or Header  |  1997-01-30  |  5KB  |  186 lines

  1. /******************************************************************************/
  2. /*                                                                            */
  3. /* ifcheck.c                                                                  */
  4. /*                                                                            */
  5. /* Check status of SANA-II interface under AmiTCP                             */
  6. /*                                                                            */
  7. /* Format:                                                                    */
  8. /*     ifcheck <interface>                                                    */
  9. /*                                                                            */
  10. /* Template:                                                                  */
  11. /*     INTERFACE/A                                                            */
  12. /*                                                                            */
  13. /* Return codes:                                                              */
  14. /*     0         if AmiTCP is active and <interface> is up                    */
  15. /*     5         if AmiTCP is inactive or <interface> is down                 */
  16. /*    10         if any fatal error occurs                                    */
  17. /*                                                                            */
  18. /* Author:                                                                    */
  19. /*     Mathew Hendry (scampi@dial.pipex.com)                                  */
  20. /*                                                                            */
  21. /*                                                                            */
  22. /******************************************************************************/
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27.  
  28. #include <exec/types.h>
  29. #include <proto/exec.h>
  30. #include <proto/dos.h>
  31.  
  32. #include <sys/types.h>
  33. #include <sys/socket.h>
  34. #include <sys/ioctl.h>
  35. #include <net/if.h>
  36.  
  37. #define IFCHECK_BUFFSIZE 10240
  38.  
  39. BOOL IsInterfaceUp( char* pIfName, BOOL* fFailed );
  40.  
  41. extern struct DosLibrary* DOSBase;
  42. struct Library*    SocketBase;
  43.  
  44. static char pTemplate[] = "INTERFACE/A";
  45. static char pVersion[] = "$VER: ifcheck 0.0 (29.1.97)\0";
  46.  
  47. int
  48. main( void )
  49. {
  50.     BOOL fFailed = FALSE;
  51.     BOOL fUp = FALSE;
  52.     int nRet = 0;
  53.  
  54.     if ((DOSBase = (struct DosLibrary*)OpenLibrary( "dos.library", 37 )) != NULL)
  55.     {
  56.         struct RDArgs* pRDArgs;
  57.         char* apArgs[ 1 ] = { 0 };
  58.  
  59.         if ((pRDArgs = ReadArgs( pTemplate, (LONG*)apArgs, NULL )) != NULL)
  60.         {
  61.             if ((SocketBase = OpenLibrary( "bsdsocket.library", 3 )) != NULL)
  62.             {
  63.                 fUp = IsInterfaceUp( apArgs[ 0 ], &fFailed ); 
  64.                 CloseLibrary( SocketBase );
  65.             }
  66.  
  67.             printf( "Interface %s is %s\n", apArgs[ 0 ], (fUp ? "up" : "down") );
  68.             FreeArgs( pRDArgs );
  69.         }
  70.         else
  71.         {
  72.             fprintf( stderr, "ifcheck: required argument missing\n" );
  73.             fFailed = TRUE;
  74.         }
  75.  
  76.         CloseLibrary( (struct Library*)DOSBase );
  77.     }
  78.     else
  79.     {
  80.         fprintf( stderr, "ifcheck: cannot open dos.library version 37 or higher\n" );
  81.         fFailed = TRUE;
  82.     }
  83.  
  84.     /* Set FAIL if anything went badly wrong */
  85.     if (fFailed)
  86.     {
  87.         nRet = 10;
  88.     }
  89.     else
  90.     {
  91.         /* Set WARN if interface is down */
  92.         if (!fUp)
  93.         {
  94.             nRet = 5;
  95.         }
  96.     }
  97.  
  98.     return nRet;
  99. }
  100.  
  101. BOOL
  102. IsInterfaceUp( char* pIfName, BOOL* fFailed )
  103. {
  104.     BOOL fUp = FALSE;
  105.     BOOL fDone = FALSE;
  106.  
  107.     int nSocket;
  108.  
  109.     /* Open socket */
  110.     if ((nSocket = socket( AF_INET, SOCK_DGRAM, 0 )) >= 0)
  111.     {
  112.         char* pBuffer;
  113.  
  114.         /* Allocate buffer to store interface configuration */
  115.         if ((pBuffer = malloc( IFCHECK_BUFFSIZE * sizeof( char ) )) != NULL)
  116.         {
  117.             struct ifconf ifConf;
  118.             ifConf.ifc_len = IFCHECK_BUFFSIZE;
  119.             ifConf.ifc_buf = pBuffer;
  120.  
  121.             /* Get interface configuration */
  122.             if (IoctlSocket( nSocket, SIOCGIFCONF, (char*)&ifConf ) >= 0)
  123.             {
  124.                 struct ifreq* pifReq = ifConf.ifc_req;
  125.                 struct ifreq* pifReqEnd = (struct ifreq*)((caddr_t)pifReq + ifConf.ifc_len);
  126.  
  127.                 /* Scan interface configuration */
  128.                 while (!fDone && !(*fFailed))
  129.                 {
  130.                     int nFudge;
  131.  
  132.                     /* Check interface name */
  133.                     if (!strcmp( pifReq->ifr_name, pIfName ))
  134.                     {
  135.                         /* Get interface flags */
  136.                         if (IoctlSocket( nSocket, SIOCGIFFLAGS, (char*)pifReq ) >= 0)
  137.                         {
  138.                             /* Check whether interface is up */
  139.                             if (pifReq->ifr_flags & IFF_UP)
  140.                             {
  141.                                 fUp = TRUE;
  142.                                 fDone = TRUE;
  143.                             }
  144.                         }
  145.                         else
  146.                         {
  147.                             fprintf( stderr, "ifcheck: SIOCGIFFLAGS request failed\n" );
  148.                             *fFailed = TRUE;
  149.                         }
  150.                     }
  151.  
  152.                     /* Fudge for variable-length addresses */
  153.                     nFudge = pifReq->ifr_addr.sa_len - sizeof( struct sockaddr );
  154.                     pifReq = (struct ifreq*)((caddr_t)pifReq + sizeof( struct ifreq ) + nFudge);
  155.  
  156.                     if (pifReq >= pifReqEnd)
  157.                     {
  158.                         fDone = TRUE;
  159.                     }
  160.                 }
  161.             }
  162.             else
  163.             {
  164.                 fprintf( stderr, "ifcheck: SIOCGIFCONF request failed\n" );
  165.                 *fFailed = TRUE;
  166.             }
  167.  
  168.             free( pBuffer );
  169.         }
  170.         else
  171.         {
  172.             fprintf( stderr, "ifcheck: failed to allocate memory\n" );
  173.             *fFailed = TRUE;
  174.         }
  175.  
  176.         CloseSocket( nSocket );
  177.     }
  178.     else
  179.     {
  180.            fprintf( stderr, "ifcheck: unable to open socket\n" );
  181.         *fFailed = TRUE;
  182.     }
  183.  
  184.     return fUp;
  185. }
  186.